from django.core.management.base import NoArgsCommand
from punbb.authent.models import PunbbGroup, PunbbUser
from punbb.forum.models import PunbbCategory, PunbbForum, PunbbTopic, PunbbPost
from django.contrib.auth.models import User, Group

from threadedcomments.models import ThreadedComment
from tribes.models import Tribe, Topic

from django.template.defaultfilters import slugify
from punbb.forum.timeutil import date_display

class Command(NoArgsCommand):
    help = 'Imports punbb forums, categories, posts.'

    requires_model_validation = False

    def handle_noargs(self, **options):
        # categories
        admin = User.objects.filter(is_superuser=True)[0]
        for cat in PunbbCategory.objects.all():
            name = cat.cat_name
            tribe, created = Tribe.objects.get_or_create(slug=slugify(name), name=name,
                                        defaults={
                                                  'creator':admin,
                                                  'description':'PunBB category',
                                                  'tags':'punbb'
                                                  })
            if created:
                 print u"Tribe %s created." % name
        for topic in PunbbTopic.objects.all():
            tribe = Tribe.objects.get(name=topic.forum.cat.cat_name)
            title = topic.subject
            title_truncated = False
            if len(title) > 50:
                title = title[:46] + ' ...'
                title_truncated = True
            tp, created = Topic.objects.get_or_create(tribe=tribe, title=title,
                                                      creator=User.objects.get(username=topic.poster),
                                                      defaults={'body':"none"})
            if created:
                tp.tags = u"punbb %s" % slugify(topic.forum.forum_name)
                tp.save()
                print 'topic %s created.' % tp.title
            
            for post in PunbbPost.objects.filter(topic=topic).order_by('posted'):
                #print post.poster_id, post.topic, post.topic.forum, post.topic.forum.cat
                
                if tp.body == "none":
                    tp.created = date_display(post.posted)
                    if post.edited:
                        tp.modified = date_display(post.edited)
                    else:
                        tp.modified = tp.created
                    tp.body = post.message
                    if title_truncated:
                        tp.body = topic.subject + '\n' + tp.body
                    tp.save()
                else:
                    tc, created = ThreadedComment.objects.get_or_create_for_object(
                                                tp,
                                                user = User.objects.get(username=post.poster),
                                                ip_address = post.poster_ip,
                                                date_submitted = date_display(post.posted),
                                                parent= None,
                                                defaults={'comment':post.message}
                                                )
                    if created:
                        print 'threaded comment %d created.' % tc.pk
        
            
